<?php
/**
 * Created by PhpStorm.
 * User: 王 朋
 * Date: 2018/4/16
 * Time: 21:57
 */
namespace application\teacher\controller;
use application\common\controller\Base;
use think\exception\HttpException;
use application\common\model\ExamineeInfo;
use application\common\model\Exam;
use think\Session;
use application\common\controller\Tools;

class Examinee extends Base
{
    public function deleteExaminee() {
        if (! $this->request->isPost()) {
            throw new HttpException('403');
        }else {
            $uniqueIDs = input('post.items/a');
            $examName = $this->request->post('examName');
            $data = [];
            foreach ($uniqueIDs as $uniqueID) {
                $data = ['uniqueID' => $uniqueID];
                ExamineeInfo::destroy($data);
            }
            $this->redirect($_SERVER['HTTP_REFERER']);
        }
    }
    public function addOneExaminee() {
        if (! $this->request->isPost()) {
            $this->assign([
                'title' => '添加考生',
                'messageCount' => 0,
            ]);
            return $this->fetch();
        }else {
            $studentID = $this->request->post('studentID');
            $studentName = $this->request->post('studentName');
            if (empty($studentID) || empty($studentName)) {
                throw new HttpException('403');
            }else {
                $exam = new Exam();
                $examineeInfo = new ExamineeInfo();
                $examID = $exam->where('exam_name',Session::get('examName'))->where('exam_creator_id',Session::get('userId'))->column('exam_id')[0];
                $examineeInfo->data([
                    'exam_id' => $examID,
                    'studentName' => $studentName,
                    'studentID' => $studentID,
                    'uniqueID' => Tools::md5Uniqid(),
                ]);
                $examineeInfo->save();
                $this->redirect('Exam/examineeInfo?examName='.Session::get('examName'));
            }
        }
    }
}